#!/bin/bash

# ============================================
# 完整测试和修复后端连接
# ============================================

echo "=== 检查 Nginx 代理配置 ==="
grep "proxy_pass" /etc/nginx/conf.d/frontend.conf | grep -v "#"

echo ""
echo "=== 测试后端直接访问 ==="
echo "测试 /api/login/:"
curl -s -o /dev/null -w "HTTP Status: %{http_code}\n" http://127.0.0.1:8000/api/login/

echo ""
echo "测试 /api/init/settings/:"
curl -s -o /dev/null -w "HTTP Status: %{http_code}\n" http://127.0.0.1:8000/api/init/settings/

echo ""
echo "=== 测试通过 Nginx 代理访问 ==="
echo "测试 /api/login/ (通过 Nginx):"
curl -s -o /dev/null -w "HTTP Status: %{http_code}\n" http://127.0.0.1/api/login/ || echo "代理失败"

echo ""
echo "测试 /api/init/settings/ (通过 Nginx):"
curl -s -o /dev/null -w "HTTP Status: %{http_code}\n" http://127.0.0.1/api/init/settings/ || echo "代理失败"

echo ""
echo "=== 检查 Nginx 错误日志 ==="
sudo tail -5 /var/log/nginx/frontend_error.log 2>/dev/null || echo "无错误日志"

echo ""
echo "=== 检查访问日志 ==="
sudo tail -5 /var/log/nginx/frontend_access.log 2>/dev/null || echo "无访问日志"

echo ""
echo "=== 如果代理失败，请确保 ==="
echo "1. Nginx 配置中的 proxy_pass 是: http://127.0.0.1:8000/api/;"
echo "2. 已执行: sudo systemctl reload nginx"
echo "3. Django 后端正在运行"

